<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String base = request.getContextPath();
    request.setAttribute("base", base);
%>
<!doctype html>
<html lang="zh" class="pu">

<head>
    <!-- meta -->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="telephone=no" name="format-detection">

    <!-- style -->
    <style>
    </style>

    <title>${title}</title>
</head>

<body style="padding: 0px;margin: 0px;display: none;">

<!-- 公众号 -->
<div id="area_wx" style="width:100%;">
    <!-- 标题 -->
    <div id="wx_div_tlt" style="width: 66%;margin-left: 17%;color: #333;padding-top: 7%;padding-bottom: 7%;text-align:center; border-bottom: solid 1px #999;">请登录您的菲遇账号</div>
    <div id="wx_div_tlta" style="width: 66%;margin-left: 17%;color: #888;padding-top: 5%;padding-bottom: 5%;text-align:center;">请选择登录方式</div>

    <!-- 操作 -->
    <div id="wx_div_wx" onclick="login_wx('${env}','${from}')" style="width: 50%;margin-left: 25%;color: #888;padding-top: 3%;padding-bottom: 3%;text-align:center;border:solid 1px #888;">当前微信号</div>
    <div id="wx_div_mob" onclick="login_mob('${title}')" style="width: 50%;margin-left: 25%;color: #888;padding-top: 3%;padding-bottom: 3%;margin-top:5%;text-align:center;border:solid 1px #888;">手机号</div>
</div>

<!-- js -->
<script src="${base}/resources/js/zepto.min.js"></script>`
<script src="${base}/resources/js/cookie_hold.js?465734423"></script>
<script src="${base}/resources/js/waiting.js"></script>
<script src="${base}/resources/js/checkstate.js"></script>
<script src="${base}/resources/js/string_hold.js?345276555"></script>

<!-- javascript -->
<script>
    var operLock = true;

    var mob_uid;
    var mob_token;
    var mob_code = "null";
    var countdown = 60;
    var app_key;

    $(document).ready(function(){
        //
        var moblogin = getCookie("moblogin");
        if (moblogin != null) {
            $('body').hide ();
            delCookie("moblogin");
            history.back();
        }

        //设备类型
        var etype = '${etype}';
        if (etype == 'iphone') app_key = 'slfdkfdks';
        else app_key = 'kdfjkdfdk';

        //计算显示尺寸
        countRect();
        $(window).resize(function () {
            countRect();
        });
        //显示内容
        $('body').show ();

        operLock = false;
    });
    function countRect () {
        //防止输入时缩放
        if($(':focus').length != 0) return;

        countRect_wx ();
    }

    function countRect_wx () {
        //尺寸自适应
        var ww;
        ww = parseInt ($(window).width());
        //标题
        $('#wx_div_tlt').css('font-size', ww * 0.045);
        $('#wx_div_tlta').css('font-size', ww * 0.04);
        //操作
        $('#wx_div_wx').css('font-size', ww * 0.04);
        $('#wx_div_wx').css('border-radius', ww * 0.03);
        $('#wx_div_mob').css('font-size', ww * 0.04);
        $('#wx_div_mob').css('border-radius', ww * 0.03);
    }

    function login_wx (env, from) {
        if (operLock) return;

        var openid = "${openid}";
        if (openid == null || openid == '') {
            alert ("微信用户信息获取失败，请通过手机号登陆");
            return;
        }
        //开发
        if (env == "dev") {
            updateWxOpenid ("114077", openid, from, "wx");
        }
        else {
            wx_login ("http://tomcat.easyifer.com/api/V25", "{'appKey':'" + app_key + "','method':'otherLoginOrRegister','token':'12345678','uid':'0','arguments':{'isApp':'2','loginType':'1','sid':'" + openid + "','unionid':'${unionid}'}}", from);
        }
    }

    function login_mob (title) {
        location.href = "${base}/common/login_mob.htm?title=" + encodeURI(encodeURI(title));
    }

    function wx_login (url, params, from) {
        ////创建请求标识信息
        var openid = "${openid}";
        var reqid = '';
        if (openid == null || openid == '') reqid = randomString(28) + "_" + new Date().getTime();
        else reqid = openid + "_" + new Date().getTime();
        //请求
        operLock = true;
        $.waiting.show('登录中...');
        $.ajax({
            url:"${base}/feiyuws/common.htm?reqid=" + reqid + "&reqname=login_wx&url=" + url + "&params=" + params,
            data: null,
            datatype: "json",
            success:function(data){
                //处理json字符串
                var json = ajaxEchoOper(data.toString());
                //
                var obj = eval("(" + json + ")");
                if (obj["status"] == 404) {
                    operLock = false;
                    $.tips ('账号不存在，请先前往APP注册账号');
                }
                else if (obj["status"] != 0) {
                    operLock = false;
                    $.tips ('微信公众号登录异常,status:' + obj["status"]);
                }
                else {
                    var uid = obj["data"]["uid"];
                    updateWxOpenid (uid, openid, from, "wx");
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                operLock = false;
                alert('请求失败,XMLHttpRequest-readyState:' + XMLHttpRequest.readyState +
                        ' XMLHttpRequest-status:' + XMLHttpRequest.status +
                        ' textStatus:' + textStatus + ' errorThrown:' + errorThrown);
            },
            complete:function(XMLHttpRequest, textStatus){
                $.waiting.hide();
            }
        });
    }

    function updateWxOpenid (uid, openid, from, tp) {
        var reqid = openid + "_" + new Date().getTime();
        $.ajax({
            url:"${base}/feiyuws/update_wx_openid.htm?reqid=" + reqid + "&reqname=update_wx_openid&uid=" + uid + "&openid=" + openid + "&tp=" + tp,
            data: null,
            datatype: "json",
            success:function(data){
                if (history.length > 1) {
                    history.go(-1);
                }
                else {
                    var url = "${base}/" + from;
                    location.href = url;
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {operLock = false;
                alert('请求失败,XMLHttpRequest-readyState:' + XMLHttpRequest.readyState +
                        ' XMLHttpRequest-status:' + XMLHttpRequest.status +
                        ' textStatus:' + textStatus + ' errorThrown:' + errorThrown);
            },
            complete:function(XMLHttpRequest, textStatus){
            }
        });
    }
</script>

</body>
</html>